package com.danskin.wechat.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.api.client.WeiXinOpenApiClient;
import com.danskin.wechat.constant.DanskinConstants;
import com.danskin.wechat.dao.WxCardConfigDao;
import com.danskin.wechat.entity.WxCardConfig;
import com.danskin.wechat.entity.WxMediaPlatform;
import com.danskin.wechat.service.WxCardConfigService;
import com.danskin.wechat.util.LogUtil;

@Service
public class WxCardConfigServiceImpl implements WxCardConfigService {

	@Resource
	WxCardConfigDao wxCardConfigDao;

	@Override
	public List<WxCardConfig> selectWxCardConfigList(WxCardConfig wxCardConfig) {
		LogUtil.info("=======根据条件查询微信优惠券配置参数：" + ToStringBuilder.reflectionToString(wxCardConfig));

		List<WxCardConfig> wxCardConfigList = new ArrayList<WxCardConfig>();

		try {
			wxCardConfigList = wxCardConfigDao.selectWxCardConfigList(wxCardConfig);
		} catch (Exception e) {
			LogUtil.error("=======根据条件查询微信优惠券配置异常", e);
		}
		return wxCardConfigList;
	}

	@Override
	public List<WxCardConfig> selectListForGiftConfig(int mpId) {
		List<WxCardConfig> wxCardConfigList = new ArrayList<WxCardConfig>();

		try {
			wxCardConfigList = wxCardConfigDao.selectListForGiftConfig(mpId);
		} catch (Exception e) {
			LogUtil.error("======为礼券配置查询优惠券模板异常", e);
		}
		return wxCardConfigList;
	}

	@Override
	public void saveMemberCardConfig(String accessToken, WxMediaPlatform entity) {
		// 查询是否已存在微信会员卡ID
		WxCardConfig cardConfig = wxCardConfigDao.selectByCardType(DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_MEMBER_CARD,
				entity.getAppId());
		if (cardConfig == null) {
			// 调用微信接口，创建优惠券
			JSONObject json = WeiXinOpenApiClient.createInstance().createMemberCard(accessToken, entity.getAppId(),
					entity.getHeadImg(), entity.getNickName());
			if (json != null && json.getString("errcode").equals("0")) {
				String cardId = json.getString("card_id");

				cardConfig = new WxCardConfig();
				cardConfig.setMpId(entity.getId());
				cardConfig.setCardId(cardId);
				cardConfig.setTemplateId(cardId);
				cardConfig.setCardType(DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_MEMBER_CARD);
				cardConfig.setName("会员卡");
				cardConfig.setStatus(1);

				wxCardConfigDao.insertSelective(cardConfig);

				// 设置开卡字段
				WeiXinOpenApiClient.createInstance().activateUserForm(accessToken, entity.getAppId(), cardId);
			}
		}
	}

	@Override
	public int saveCouponConfig(WxCardConfig entity, WxMediaPlatform mp) {
		WxCardConfig cardConfig = wxCardConfigDao.selectByTemplateId(mp.getId(), entity.getTemplateId());
		if (cardConfig == null) {

			long beginTime = entity.getStartTime().getTime() / 1000;
			long endTime = entity.getExpiredTime().getTime() / 1000;

			JSONObject json = null;
			if (entity.getCardType().equals(DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_CASH)) {

				json = WeiXinOpenApiClient.createInstance().createCashCoupon(mp.getAccessToken(), mp.getAppId(),
						mp.getHeadImg(), mp.getNickName(), entity.getName(), entity.getRuleDesc(), beginTime, endTime,
						new BigDecimal(0), entity.getFaceValue());
				if (json != null && json.getString("errcode").equals("0")) {
					entity.setCardId(json.getString("card_id"));
				} else {
					return -1;
				}
			} else if (entity.getCardType().equals(DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_DISCOUNT)) {

				json = WeiXinOpenApiClient.createInstance().createDiscountCoupon(mp.getAccessToken(), mp.getAppId(),
						mp.getHeadImg(), mp.getNickName(), entity.getName(), entity.getRuleDesc(), beginTime, endTime,
						entity.getFaceValue());
				if (json != null && json.getString("errcode").equals("0")) {
					entity.setCardId(json.getString("card_id"));
				} else {
					return -1;
				}
			} else if (entity.getCardType().equals(DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_ECCOUPON)) {
				entity.setCardId(DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_ECCOUPON);
			}

			return wxCardConfigDao.insertSelective(entity);
		} else {
			entity.setId(cardConfig.getId());
			entity.setLastModifiedTime(new Date());
			return wxCardConfigDao.updateByPrimaryKeySelective(entity);
		}
	}

}
